<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model\Backend\ProjectType;
use App\Model\Backend\ProjectTypeRole;

class RoleController extends Controller
{
	function __construct(Request $req){
		
		$session = $req->session()->get('member');
		if($session['gid'] != 1) abort(403,'Unauthorized action.');
	}
	
	function index(){
		$member = DB::table('member')->orderBy('uid','asc')->get();
		return view('backend.role.index',compact('member'));
	}

	function show(){
		$member = DB::table('member')->where('uid',1)->get();
		print_R($member);
		echo 'show';
	}

	function roleList(){

		$deptsTree = $depts = $temp = [];

		$project_type = DB::table('project_type')->select('type_id as id','name')->get();
		$project_type_role = DB::table('project_type_role')->select('rid as id','type_id', 'name', 'is_auditor')->get();

		foreach($project_type_role as $k => $dt){

			$dt->childDepartment = [];
			$temp[$dt->type_id][] = (array) $dt;
		}

		foreach($project_type as $k => $dt){
			$depts[$k] = (array) $dt;
			$deptsTree[$k] = (array) $dt;

			if( isset($temp[$dt->id]) ){
				$deptsTree[$k]['childDepartment'] = $temp[$dt->id];
			}else{
				$deptsTree[$k]['childDepartment'] = '';
			}
		}

		$json = ['depts'=>$depts, 'deptsTree'=> $deptsTree];

		echo json_encode($json);
	}

	function store(Request $request){
		$form = $request->all();
		// print_R($form);exit;

		if( (int) $form['id']){

			//编辑
			DB::table('project_type')->where('type_id', $form['id'])->update( ['name' => $form['name']] );
		}else{
			if($form['fid'] == 1){ //插入项目类型

				$data = ['name' => $form['name']];
				// $id = ProjectType::insertGetId($data);
				$id = DB::table('project_type')->insertGetId($data);

				if($id){
					ProjectTypeRole::insert([
							'name'=> '专家审核',
							'type_id'=> $id,
							'is_auditor'=> 1,
							'auditor_level'=> 1
					]);
					ProjectTypeRole::insert([
							'name'=> '商务审核',
							'type_id'=> $id,
							'is_auditor'=> 1,
							'auditor_level'=> 2
					]);
					ProjectTypeRole::insert([
							'name'=> '客户审核',
							'type_id'=> $id,
							'is_auditor'=> 1,
							'auditor_level'=> 3
					]);
				}

			}elseif($form['fid'] == 0){ //插入项目角色

				$data = [
						'name' => $form['name'],
						'type_id' => $form['pid']
				];

				DB::table('project_type_role')->insert($data);

				// $flight = new ProjectTypeRole;
				// $flight->name =$form['name'];
				// $flight->save();
			}

		}
	}

	function destroy(Request $request, $id){

		$form = $request->all();

		if($form['fid'] == '-1'){ //删除角色

			ProjectTypeRole::destroy($id);

		}elseif($form['fid'] == 0){//删除类型 uid

			ProjectTypeRole::where('type_id', $id)->delete();
			ProjectType::destroy($id);
		}

	}

	//添加/编辑角色
	function editStaff(Request $request){

		$form = $request->all();

		// 删除旧的
		DB::delete('delete from xz_member_role where rid='. $form['roleId']);

		//添加新的
		$uidarr = explode(',', $form['uidArr']);

		foreach($uidarr as $uid){

			if($uid)
				DB::table('member_role')->insert([ 'uid' => $uid, 'rid' => $form['roleId']]);
		}

	}

	// 获取角色列表
	function getRoleStaff(Request $request){

		$roleId = $request->input('roleId');
		$member = $staff = [];

		if($roleId){

			$arr = [];
			$role = DB::table('member_role')->where('rid', $roleId)->get();

			foreach($role as $ro)
				$arr[] = $ro->uid;

			if( !empty($arr) ){

				$staff = DB::select('select * from xz_member where uid in('. implode(',', $arr) .')');
			}

		}else{
			$staff = DB::table('member')->orderBy('uid','asc')->get();
		}

		foreach($staff as $f)
			$member[] = (array) $f;

		echo json_encode($member);
	}


	public function changerole(){
		return view('backend.role.changerole');
	}

}
